home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / objects-x.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-11  |  30.6 KB  |  980 lines

  1. /* X-specific Lisp objects.
  2.    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  3.    Copyright (C) 1995 Board of Trustees, University of Illinois
  4.    Copyright (C) 1995 Tinker Systems
  5.    Copyright (C) 1995 Ben Wing
  6.  
  7. This file is part of XEmacs.
  8.  
  9. XEmacs is free software; you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the
  11. Free Software Foundation; either version 2, or (at your option) any
  12. later version.
  13.  
  14. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with XEmacs; see the file COPYING.  If not, write to the Free
  21. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. /* Synched up with: Not in FSF. */
  24.  
  25. /* Authors: Jamie Zawinski, Chuck Thompson, Ben Wing */
  26.  
  27. #include <config.h>
  28. #include "lisp.h"
  29.  
  30. #include "device-x.h"
  31. #include "objects-x.h"
  32.  
  33. #include "buffer.h"
  34. #include "insdel.h"
  35.  
  36. int handle_nonfull_spec_fonts;
  37.  
  38.  
  39. /************************************************************************/
  40. /*                          color instances                             */
  41. /************************************************************************/
  42.  
  43. /* Replacement for XAllocColor() that tries to return the nearest
  44.    available color if the colormap is full.  From FSF Emacs. */
  45.  
  46. int
  47. allocate_nearest_color (Display *display, Colormap screen_colormap,
  48.                 XColor *color_def)
  49. {
  50.   int status;
  51.  
  52.   status = XAllocColor (display, screen_colormap, color_def);
  53.   if (!status)
  54.     {
  55.       /* If we got to this point, the colormap is full, so we're
  56.      going to try and get the next closest color.
  57.      The algorithm used is a least-squares matching, which is
  58.      what X uses for closest color matching with StaticColor visuals.  */
  59.  
  60.       XColor *cells;
  61.       int no_cells;
  62.       int nearest;
  63.       long nearest_delta, trial_delta;
  64.       int x;
  65.  
  66.       no_cells = XDisplayCells (display, XDefaultScreen (display));
  67.       cells = (XColor *) alloca (sizeof (XColor) * no_cells);
  68.  
  69.       for (x = 0; x < no_cells; x++)
  70.     cells[x].pixel = x;
  71.  
  72.       XQueryColors (display, screen_colormap, cells, no_cells);
  73.       nearest = 0;
  74.       /* I'm assuming CSE so I'm not going to condense this. */
  75.       nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8))
  76.             * ((color_def->red >> 8) - (cells[0].red >> 8)))
  77.                +
  78.                (((color_def->green >> 8) - (cells[0].green >> 8))
  79.             * ((color_def->green >> 8) - (cells[0].green >> 8)))
  80.                +
  81.                (((color_def->blue >> 8) - (cells[0].blue >> 8))
  82.             * ((color_def->blue >> 8) - (cells[0].blue >> 8))));
  83.       for (x = 1; x < no_cells; x++)
  84.     {
  85.       trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8))
  86.               * ((color_def->red >> 8) - (cells[x].red >> 8)))
  87.              +
  88.              (((color_def->green >> 8) - (cells[x].green >> 8))
  89.               * ((color_def->green >> 8) - (cells[x].green >> 8)))
  90.              +
  91.              (((color_def->blue >> 8) - (cells[x].blue >> 8))
  92.               * ((color_def->blue >> 8) - (cells[x].blue >> 8))));
  93.       if (trial_delta < nearest_delta)
  94.         {
  95.           nearest = x;
  96.           nearest_delta = trial_delta;
  97.         }
  98.     }
  99.       color_def->red = cells[nearest].red;
  100.       color_def->green = cells[nearest].green;
  101.       color_def->blue = cells[nearest].blue;
  102.       status = XAllocColor (display, screen_colormap, color_def);
  103.     }
  104.  
  105.   return status;
  106. }
  107.  
  108. static int
  109. x_initialize_color_instance (struct Lisp_Color_Instance *c, Lisp_Object name,
  110.                  Lisp_Object device, int no_error)
  111. {
  112.   Display *dpy;
  113.   Screen *xs;
  114.   XColor color;
  115.   Colormap cmap;
  116.   int result;
  117.   
  118.   dpy = DEVICE_X_DISPLAY (XDEVICE (device));
  119.   xs = DefaultScreenOfDisplay (dpy);
  120.   cmap = DefaultColormapOfScreen (xs);
  121.   
  122.   memset (&color, 0, sizeof (color));
  123.   result = XParseColor (dpy, cmap,
  124.             string_ext_data (XSTRING (c->name)),
  125.             &color);
  126.   if (!result)
  127.     {
  128.       if (no_error)
  129.     return 0;
  130.       else
  131.     signal_simple_error ("unrecognised color", c->name);
  132.     }
  133.   result = allocate_nearest_color (dpy, cmap, &color);
  134.   if (!result)
  135.     {
  136.       if (no_error)
  137.     return 0;
  138.       else
  139.     signal_simple_error ("couldn't allocate color", c->name);
  140.     }
  141.  
  142.   /* Don't allocate the data until we're sure that we will succeed,
  143.      or the finalize method may get fucked. */
  144.   c->data = malloc_type (struct x_color_instance_data);
  145.   COLOR_INSTANCE_X_COLOR (c) = color;
  146.   return 1;
  147. }
  148.  
  149. static void
  150. x_print_color_instance (struct Lisp_Color_Instance *c,
  151.             Lisp_Object printcharfun,
  152.             int escapeflag)
  153. {
  154.   char buf[100];
  155.   XColor color = COLOR_INSTANCE_X_COLOR (c);
  156.   sprintf (buf, " %ld=(%X,%X,%X)",
  157.        color.pixel, color.red, color.green, color.blue);
  158.   write_c_string (buf, printcharfun);
  159. }
  160.  
  161. static void
  162. x_finalize_color_instance (struct Lisp_Color_Instance *c)
  163. {
  164.   Display *dpy = DEVICE_X_DISPLAY (XDEVICE (c->device));
  165.   
  166.   if (c->data)
  167.     {
  168.       XFreeColors (dpy,
  169.            DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy)),
  170.            &COLOR_INSTANCE_X_COLOR (c).pixel, 1, 0);
  171.       xfree (c->data);
  172.       c->data = 0;
  173.     }
  174. }
  175.  
  176. /* Color instances are equal if they resolve to the same color on the
  177.    screen (have the same RGB values).  I imagine that
  178.    "same RGV values" == "same cell in the colormap."  Arguably we should
  179.    be comparing their names instead. */
  180.  
  181. static int
  182. x_color_instance_equal (struct Lisp_Color_Instance *c1,
  183.             struct Lisp_Color_Instance *c2,
  184.             int depth)
  185. {
  186.   XColor color1 = COLOR_INSTANCE_X_COLOR (c1);
  187.   XColor color2 = COLOR_INSTANCE_X_COLOR (c2);
  188.   return ((color1.red == color2.red) &&
  189.       (color1.green == color2.green) &&
  190.       (color1.blue == color2.blue));
  191. }
  192.  
  193. static unsigned long
  194. x_color_instance_hash (struct Lisp_Color_Instance *c, int depth)
  195. {
  196.   XColor color = COLOR_INSTANCE_X_COLOR (c);
  197.   return HASH3 (color.red, color.green, color.blue);
  198. }
  199.  
  200. static Lisp_Object
  201. x_color_instance_rgb_components (struct Lisp_Color_Instance *c)
  202. {
  203.   XColor color = COLOR_INSTANCE_X_COLOR (c);
  204.   return (list3 (make_number (color.red), 
  205.          make_number (color.green),
  206.          make_number (color.blue)));
  207. }
  208.  
  209. static int
  210. x_valid_color_name_p (struct device *d, Lisp_Object color)
  211. {
  212.   XColor c;
  213.   Display *dpy = DEVICE_X_DISPLAY (d);
  214.  
  215.   return XParseColor (dpy,
  216.               DefaultColormapOfScreen (DefaultScreenOfDisplay (dpy)),
  217.               string_ext_data (XSTRING (color)), &c);
  218. }
  219.  
  220.  
  221. /************************************************************************/
  222. /*                           font instances                             */
  223. /************************************************************************/
  224.  
  225. static int
  226. x_initialize_font_instance (struct Lisp_Font_Instance *f, Lisp_Object name,
  227.                  Lisp_Object device, int no_error)
  228. {
  229.   Display *dpy;
  230.   unsigned int def_char;
  231. #ifdef MULE
  232.   XFontSet xf;
  233.   char **missing_charset_list = 0;
  234.   int missing_charset_count = 0;
  235.   char *def_string = "";
  236. #else
  237.   XFontStruct *xf;
  238. #endif
  239.   
  240.   dpy = DEVICE_X_DISPLAY (XDEVICE (device));
  241.   
  242. #ifdef MULE
  243.   xf = XCreateFontSet (dpy, string_ext_data (XSTRING (f->name)),
  244.                &missing_charset_list, &missing_charset_count,
  245.                &def_string);
  246. #else
  247.   xf = XLoadQueryFont (dpy, string_ext_data (XSTRING (f->name)));
  248. #endif
  249.   
  250.   if (!xf)
  251.     {
  252.       if (no_error)
  253.     return 0;
  254. #ifdef MULE
  255.       if (!missing_charset_list)
  256.     signal_simple_error ("couldn't load font set", f->name);
  257.       else
  258.     {
  259.       Lisp_Object lost = Qnil;
  260.       int i;
  261.       for (i = missing_charset_count-1; i >= 0; i--)
  262.         lost = Fcons (build_string (missing_charset_list[i]),
  263.               lost);
  264.       signal_simple_error_2
  265.         ("couldn't load font set (missing required fonts)",
  266.          f->name, lost);
  267.     }
  268. #else
  269.       signal_simple_error ("couldn't load font", f->name);
  270. #endif
  271.     }
  272.   
  273. #ifdef MULE
  274.   if (!XExtentsOfFontSet (xf)->max_logical_extent.width)
  275. #else
  276.     if (!xf->max_bounds.width)
  277. #endif
  278.       {
  279.     /* yes, this has been known to happen. */
  280. #ifdef MULE
  281.     XFreeFontSet (dpy, xf);
  282. #else
  283.     XFreeFont (dpy, xf);
  284. #endif
  285.     if (no_error)
  286.       return 0;
  287.     signal_simple_error ("X font is too small", f->name);
  288.       }
  289.   
  290.   /* Don't allocate the data until we're sure that we will succeed,
  291.      or the finalize method may get fucked. */
  292.   f->data = malloc_type (struct x_font_instance_data);
  293.   FONT_INSTANCE_X_TRUENAME (f) = Qnil;
  294.   FONT_INSTANCE_X_FONT (f) = xf;
  295. #ifdef MULE
  296.   f->height = XExtentsOfFontSet(xf)->max_logical_extent.height;
  297.   f->ascent = -XExtentsOfFontSet(xf)->max_logical_extent.y;
  298.   f->descent = xf->height - xf->ascent;
  299. #else
  300.   f->ascent = xf->ascent;
  301.   f->descent = xf->descent;
  302.   f->height = xf->ascent + xf->descent;
  303. #endif
  304.   /* We used to use 'N' as the default character but that ends up
  305.      being a little wide as a default when variable width fonts are
  306.      used.  'n' seems to give a much better average. */
  307. #ifdef MULE
  308.   {
  309.     XFontStruct **fstruct_l;
  310.     char **fname_l;
  311.     int n;
  312.     n= XFontsOfFontSet (xf, &fstruct_l, &fname_l);
  313.     if (n == 0) 
  314.       abort ();
  315.     /* first font is iso-8859 and that's the width that we want to use */
  316.     lf->width = fstruct_l[0]->max_bounds.width;
  317.     def_char =
  318.       ((fstruct_l[0]->default_char >= fstruct_l[0]->min_char_or_byte2 &&
  319.     fstruct_l[0]->default_char <= fstruct_l[0]->max_char_or_byte2)
  320.        ? fstruct_l[0]->default_char
  321.        : 'n');
  322.   }
  323. #else
  324.   /* Old versions of the R5 font server have garbage (>63k) as def_char. */
  325.   def_char = ((xf->default_char >= xf->min_char_or_byte2 &&
  326.            xf->default_char <= xf->max_char_or_byte2)
  327.           ? xf->default_char
  328.           : 'n');
  329.  once_more:
  330.   f->width = (xf->per_char
  331.           /* #### what are we supposed to do with byte1 here? */
  332.           ? xf->per_char [def_char - xf->min_char_or_byte2].width
  333.           : xf->max_bounds.width);
  334.   
  335.   /* Some fonts have a default char whose width is 0.  This is no good.
  336.      If that's the case, first try 'n' as the default char, and if n has
  337.      0 width too (unlikely) then just use the max width. */
  338.   if (f->width == 0)
  339.     {
  340.       if (def_char == 'n')
  341.     f->width = xf->max_bounds.width;
  342.       else
  343.     {
  344.       def_char = 'n';
  345.       goto once_more;
  346.     }
  347.     }
  348. #endif
  349. #ifdef MULE
  350.   /* !!#### Review this. */
  351.   f->proportional_p = 1;
  352. #else
  353.   /* If all characters don't exist then there could potentially be
  354.      0-width characters lurking out there.  Not setting this flag
  355.      trips an optimization that would make them appear to have width
  356.      to redisplay.  This is bad.  So we set it if not all characters
  357.      have the same width or if not all characters are defined.
  358.      */
  359.   /* #### This sucks.  There is a measurable performance increase
  360.      when using proportional width fonts if this flag is not set.
  361.      Unfortunately so many of the fucking X fonts are not fully
  362.      defined that we could almost just get rid of this damn flag and
  363.      make it an assertion. */
  364.   f->proportional_p = (xf->min_bounds.width != xf->max_bounds.width ||
  365.                (handle_nonfull_spec_fonts &&
  366.             !xf->all_chars_exist));
  367. #endif
  368.  
  369.   return 1;
  370. }
  371.  
  372. static void
  373. x_mark_font_instance (struct Lisp_Font_Instance *f,
  374.                void (*markobj) (Lisp_Object))
  375. {
  376.   ((markobj) (FONT_INSTANCE_X_TRUENAME (f)));
  377. }
  378.  
  379. static void
  380. x_print_font_instance (struct Lisp_Font_Instance *f,
  381.                Lisp_Object printcharfun,
  382.                int escapeflag)
  383. {
  384.   char buf[200];
  385.   sprintf (buf, " 0x%lx", (unsigned long) FONT_INSTANCE_X_FONT (f)->fid);
  386.   write_c_string (buf, printcharfun);
  387. }
  388.  
  389. static void
  390. x_finalize_font_instance (struct Lisp_Font_Instance *f)
  391. {
  392.   Display *dpy = DEVICE_X_DISPLAY (XDEVICE (f->device));
  393.   
  394.   if (f->data)
  395.     {
  396. # ifdef MULE
  397.       XFreeFontSet (dpy, FONT_INSTANCE_X_FONT (f));
  398. # else
  399.       XFreeFont (dpy, FONT_INSTANCE_X_FONT (f));
  400. # endif
  401.       xfree (f->data);
  402.       f->data = 0;
  403.     }
  404. }
  405.  
  406. /* Determining the truename of a font is hard.  (Big surprise.)
  407.  
  408.    By "truename" we mean an XLFD-form name which contains no wildcards, yet
  409.    which resolves to *exactly* the same font as the one which we already have
  410.    the (probably wildcarded) name and `XFontStruct' of.
  411.  
  412.    One might think that the first font returned by XListFonts would be the one
  413.    that XOpenFont would pick.  Apparently this is the case on some servers,
  414.    but not on others.  It would seem not to be specified.
  415.  
  416.    The MIT R5 server sometimes appears to be picking the lexicographically
  417.    smallest font which matches the name (thus picking "adobe" fonts before
  418.    "bitstream" fonts even if the bitstream fonts are earlier in the path, and
  419.    also picking 100dpi adobe fonts over 75dpi adobe fonts even though the
  420.    75dpi are in the path earlier) but sometimes appears to be doing something
  421.    else entirely (for example, removing the bitsream fonts from the path will
  422.    cause the 75dpi adobe fonts to be used instead of the100dpi, even though
  423.    their relative positions in the path (and their names!) have not changed).
  424.  
  425.    The documentation for XSetFontPath() seems to indicate that the order of
  426.    entries in the font path means something, but it's pretty noncommital about
  427.    it, and the spirit of the law is apparently not being obeyed...
  428.  
  429.    All the fonts I've seen have a property named `FONT' which contains the
  430.    truename of the font.  However, there are two problems with using this: the
  431.    first is that the X Protocol Document is quite explicit that all properties
  432.    are optional, so we can't depend on it being there.  The second is that
  433.    it's concievable that this alleged truename isn't actually accessible as a
  434.    font, due to some difference of opinion between the font designers and
  435.    whoever installed the font on the system.
  436.  
  437.    So, our first attempt is to look for a FONT property, and then verify that
  438.    the name there is a valid name by running XListFonts on it.  There's still
  439.    the potential that this could be true but we could still be being lied to,
  440.    but that seems pretty remote.
  441.  
  442.      Late breaking news: I've gotten reports that SunOS 4.1.3U1
  443.      with OpenWound 3.0 has a font whose truename is really
  444.      "-Adobe-Courier-Medium-R-Normal--12-120-75-75-M-70-ISO8859-1"
  445.      but whose FONT property contains "Courier".
  446.  
  447.      So we disbelieve the FONT property unless it begins with a dash and
  448.      is more than 30 characters long.  X Windows: The defacto substandard.
  449.      X Windows: Complex nonsolutions to simple nonproblems.  X Windows:
  450.      Live the nightmare.
  451.  
  452.    If the FONT property doesn't exist, then we try and construct an XLFD name
  453.    out of the other font properties (FOUNDRY, FAMILY_NAME, WEIGHT_NAME, etc).
  454.    This is necessary at least for some versions of OpenWound.  But who knows
  455.    what the future will bring.
  456.  
  457.    If that doesn't work, then we use XListFonts and either take the first font
  458.    (which I think is the most sensible thing) or we find the lexicographically
  459.    least, depending on whether the preprocessor constant `XOPENFONT_SORTS' is
  460.    defined.  This sucks because the two behaviors are a property of the server
  461.    being used, not the architecture on which emacs has been compiled.  Also,
  462.    as I described above, sorting isn't ALWAYS what the server does.  Really it
  463.    does something seemingly random.  There is no reliable way to win if the
  464.    FONT property isn't present.
  465.  
  466.    Another possibility which I haven't bothered to implement would be to map
  467.    over all of the matching fonts and find the first one that has the same
  468.    character metrics as the font we already have loaded.  Even if this didn't
  469.    return exactly the same font, it would at least return one whose characters
  470.    were the same sizes, which would probably be good enough.
  471.  
  472.    More late-breaking news: on RS/6000 AIX 3.2.4, the expression
  473.         XLoadQueryFont (dpy, "-*-Fixed-Medium-R-*-*-*-130-75-75-*-*-ISO8859-1")
  474.    actually returns the font
  475.         -Misc-Fixed-Medium-R-Normal--13-120-75-75-C-80-ISO8859-1
  476.    which is crazy, because that font doesn't even match that pattern!  It is
  477.    also not included in the output produced by `xlsfonts' with that pattern.
  478.  
  479.    So this is yet another example of XListFonts() and XOpenFont() using
  480.    completely different algorithms.  This, however, is a goofier example of
  481.    this bug, because in this case, it's not just the search order that is 
  482.    different -- the sets don't even intersect.
  483.  
  484.    If anyone has any better ideas how to do this, or any insights on what it is
  485.    that the various servers are actually doing, please let me know!  -- jwz. */
  486.  
  487. static int
  488. valid_x_font_name_p (Display *dpy, char *name)
  489. {
  490.   /* Maybe this should be implemented by callign XLoadFont and trapping
  491.      the error.  That would be a lot of work, and wasteful as hell, but
  492.      might be more correct.
  493.    */
  494.   int nnames = 0;
  495.   char **names;
  496.   if (! name)
  497.     return 0;
  498.   names = XListFonts (dpy, name, 1, &nnames);
  499.   if (names)
  500.     XFreeFontNames (names);
  501.   return (nnames != 0);
  502. }
  503.  
  504. static char *
  505. truename_via_FONT_prop (Display *dpy, XFontStruct *font)
  506. {
  507.   unsigned long value = 0;
  508.   char *result = 0;
  509.   if (XGetFontProperty (font, XA_FONT, &value))
  510.     result = XGetAtomName (dpy, value);
  511.   /* result is now 0, or the string value of the FONT property. */
  512.   if (result)
  513.     {
  514.       /* Verify that result is an XLFD name (roughly...) */
  515.       if (result [0] != '-' || strlen (result) < (unsigned int) 30)
  516.     {
  517.       XFree (result);
  518.       result = 0;
  519.     }
  520.     }
  521.   return result;    /* this must be freed by caller if non-0 */
  522. }
  523.  
  524. static char *
  525. truename_via_random_props (Display *dpy, XFontStruct *font)
  526. {
  527.   struct device *d = get_device_from_display (dpy);
  528.   unsigned long value = 0;
  529.   char *foundry, *family, *weight, *slant, *setwidth, *add_style;
  530.   unsigned long pixel, point, res_x, res_y;
  531.   char *spacing;
  532.   unsigned long avg_width;
  533.   char *registry, *encoding;
  534.   char composed_name [2048];
  535.   int ok = 0;
  536.   char *result;
  537.  
  538. #define get_string(atom,var)                \
  539.   if (XGetFontProperty (font, (atom), &value))        \
  540.     var = XGetAtomName (dpy, value);            \
  541.   else    {                        \
  542.     var = 0;                        \
  543.     goto FAIL; }
  544. #define get_number(atom,var)                \
  545.   if (!XGetFontProperty (font, (atom), &var) ||        \
  546.       var > 999)                    \
  547.     goto FAIL;
  548.  
  549.   foundry = family = weight = slant = setwidth = 0;
  550.   add_style = spacing = registry = encoding = 0;
  551.  
  552.   get_string (DEVICE_XATOM_FOUNDRY (d), foundry);
  553.   get_string (DEVICE_XATOM_FAMILY_NAME (d), family);
  554.   get_string (DEVICE_XATOM_WEIGHT_NAME (d), weight);
  555.   get_string (DEVICE_XATOM_SLANT (d), slant);
  556.   get_string (DEVICE_XATOM_SETWIDTH_NAME (d), setwidth);
  557.   get_string (DEVICE_XATOM_ADD_STYLE_NAME (d), add_style);
  558.   get_number (DEVICE_XATOM_PIXEL_SIZE (d), pixel);
  559.   get_number (DEVICE_XATOM_POINT_SIZE (d), point);
  560.   get_number (DEVICE_XATOM_RESOLUTION_X (d), res_x);
  561.   get_number (DEVICE_XATOM_RESOLUTION_Y (d), res_y);
  562.   get_string (DEVICE_XATOM_SPACING (d), spacing);
  563.   get_number (DEVICE_XATOM_AVERAGE_WIDTH (d), avg_width);
  564.   get_string (DEVICE_XATOM_CHARSET_REGISTRY (d), registry);
  565.   get_string (DEVICE_XATOM_CHARSET_ENCODING (d), encoding);
  566. #undef get_number
  567. #undef get_string
  568.  
  569.   sprintf (composed_name,
  570.        "-%s-%s-%s-%s-%s-%s-%ld-%ld-%ld-%ld-%s-%ld-%s-%s",
  571.        foundry, family, weight, slant, setwidth, add_style, pixel,
  572.        point, res_x, res_y, spacing, avg_width, registry, encoding);
  573.   ok = 1;
  574.  
  575.  FAIL:
  576.   if (ok)
  577.     {
  578.       int L = strlen (composed_name) + 1;
  579.       result = xmalloc (L);
  580.       strncpy (result, composed_name, L);
  581.     }
  582.   else
  583.     result = 0;
  584.  
  585.   if (foundry) XFree (foundry);
  586.   if (family) XFree (family);
  587.   if (weight) XFree (weight);
  588.   if (slant) XFree (slant);
  589.   if (setwidth) XFree (setwidth);
  590.   if (add_style) XFree (add_style);
  591.   if (spacing) XFree (spacing);
  592.   if (registry) XFree (registry);
  593.   if (encoding) XFree (encoding);
  594.  
  595.   return result;
  596. }
  597.  
  598. /* Unbounded, for sufficiently small values of infinity... */
  599. #define MAX_FONT_COUNT 5000
  600.  
  601. static char *
  602. truename_via_XListFonts (Display *dpy, char *font_name)
  603. {
  604.   char *result = 0;
  605.   char **names;
  606.   int count = 0;
  607.  
  608. #ifndef XOPENFONT_SORTS
  609.   /* In a sensible world, the first font returned by XListFonts()
  610.      would be the font that XOpenFont() would use.  */
  611.   names = XListFonts (dpy, font_name, 1, &count);
  612.   if (count) result = names [0];
  613. #else
  614.   /* But the world I live in is much more perverse. */
  615.   names = XListFonts (dpy, font_name, MAX_FONT_COUNT, &count);
  616.   while (count--)
  617.     /* If names[count] is lexicographically less than result, use it.
  618.        (#### Should we be comparing case-insensitively?) */
  619.     if (result == 0 || (strcmp (result, names [count]) < 0))
  620.       result = names [count];
  621. #endif
  622.  
  623.   if (result)
  624.     result = xstrdup (result);
  625.   if (names)
  626.     XFreeFontNames (names);
  627.  
  628.   return result;    /* this must be freed by caller if non-0 */
  629. }
  630.  
  631. static Lisp_Object
  632. x_font_truename (Display *dpy, char *name, XFontStruct *font)
  633. {
  634.   char *truename_FONT = 0;
  635.   char *truename_random = 0;
  636.   char *truename = 0;
  637.   
  638.   /* The search order is:
  639.      - if FONT property exists, and is a valid name, return it.
  640.      - if the other props exist, and add up to a valid name, return it.
  641.      - if we find a matching name with XListFonts, return it.
  642.      - if FONT property exists, return it regardless.
  643.      - if other props exist, return the resultant name regardless.
  644.      - else return 0.
  645.    */
  646.  
  647.   truename = truename_FONT = truename_via_FONT_prop (dpy, font);
  648.   if (truename && !valid_x_font_name_p (dpy, truename))
  649.     truename = 0;
  650.   if (!truename)
  651.     truename = truename_random = truename_via_random_props (dpy, font);
  652.   if (truename && !valid_x_font_name_p (dpy, truename))
  653.     truename = 0;
  654.   if (!truename && name)
  655.     truename = truename_via_XListFonts (dpy, name);
  656.  
  657.   if (!truename)
  658.     {
  659.       /* Gag - we weren't able to find a seemingly-valid truename.
  660.      Well, maybe we're on one of those braindead systems where
  661.      XListFonts() and XLoadFont() are in violent disagreement.
  662.      If we were able to compute a truename, try using that even
  663.      if evidence suggests that it's not a valid name - because
  664.      maybe it is, really, and that's better than nothing.
  665.      X Windows: You'll envy the dead.
  666.        */
  667.       if (truename_FONT)
  668.     truename = truename_FONT;
  669.       else if (truename_random)
  670.     truename = truename_random;
  671.     }
  672.  
  673.   /* One or both of these are not being used - free them. */
  674.   if (truename_FONT && truename_FONT != truename)
  675.     XFree (truename_FONT);
  676.   if (truename_random && truename_random != truename)
  677.     XFree (truename_random);
  678.  
  679.   if (truename)
  680.     {
  681.       Lisp_Object result = build_string (truename);
  682.       xfree (truename);
  683.       return result;
  684.     }
  685.   else
  686.     return Qnil;
  687. }
  688.  
  689. static Lisp_Object
  690. x_font_instance_truename (struct Lisp_Font_Instance *f, int no_error)
  691. {
  692.   struct device *d = XDEVICE (f->device);
  693.  
  694.   if (NILP (FONT_INSTANCE_X_TRUENAME (f)))
  695.     {
  696.       Display *dpy = DEVICE_X_DISPLAY (d);
  697.       char *name =
  698.     (char *) string_data (XSTRING (f->name));
  699. #ifdef MULE
  700.       {
  701.     XFontStruct **fonts;
  702.     char **names;
  703.     int count = XFontsOfFontSet (FONT_INSTANCE_X_FONT (f), &fonts, &names);
  704.     Lisp_Object *lnames =
  705.       (Lisp_Object *) alloca (count * 2 * sizeof (Lisp_Object));
  706.     Lisp_Object comma = build_string (","); /* Q'ing this would be silly */
  707.     int i, j;
  708.     for (i = 0, j = 0; i < count; i++)
  709.       {
  710.         Lisp_Object tn = x_font_truename (dpy, names [i], fonts [i]);
  711.         if (NILP (tn)) tn = build_string (names [i]);
  712.         if (i != 0) lnames [j++] = comma;
  713.         lnames [j++] = tn;
  714.       }
  715.     FONT_INSTANCE_X_TRUENAME (f) = Fconcat (j, lnames);
  716.       }
  717. #else
  718.       {
  719.     FONT_INSTANCE_X_TRUENAME (f) =
  720.       x_font_truename (dpy, name, FONT_INSTANCE_X_FONT (f));
  721.       }
  722. #endif
  723.       if (NILP (FONT_INSTANCE_X_TRUENAME (f)))
  724.     {
  725.       if (no_error)
  726.         /* Ok, just this once, return the font name as the truename.
  727.            (This is only used by Fequal() right now.) */
  728.         return f->name;
  729.       else
  730.         {
  731.           Lisp_Object font_instance = Qnil;
  732.           XSETFONT_INSTANCE (font_instance, f);
  733.           signal_simple_error ("couldn't determine font truename",
  734.                    font_instance);
  735.         }
  736.     }
  737.     }
  738.   return (FONT_INSTANCE_X_TRUENAME (f));
  739. }
  740.  
  741. static Lisp_Object
  742. x_font_instance_properties (struct Lisp_Font_Instance *f)
  743. {
  744.   struct device *d = XDEVICE (f->device);
  745.   int i;
  746.   Lisp_Object result = Qnil;
  747.   XFontProp *props;
  748.   Display *dpy;
  749.   
  750.   dpy = DEVICE_X_DISPLAY (d);
  751.   /* #### Won't work under Mule. */
  752.   props = FONT_INSTANCE_X_FONT (f)->properties;
  753.   for (i = FONT_INSTANCE_X_FONT (f)->n_properties - 1; i >= 0; i--)
  754.     {
  755.       char *name_str = 0;
  756.       char *val_str = 0;
  757.       Lisp_Object name, value;
  758.       Atom atom = props [i].name;
  759.       name_str = XGetAtomName (dpy, atom);
  760.       name = (name_str ? intern (name_str) : Qnil);
  761.       if (name_str &&
  762.       (atom == XA_FONT ||
  763.        atom == DEVICE_XATOM_FOUNDRY (d) ||
  764.        atom == DEVICE_XATOM_FAMILY_NAME (d) ||
  765.        atom == DEVICE_XATOM_WEIGHT_NAME (d) ||
  766.        atom == DEVICE_XATOM_SLANT (d) ||
  767.        atom == DEVICE_XATOM_SETWIDTH_NAME (d) ||
  768.        atom == DEVICE_XATOM_ADD_STYLE_NAME (d) ||
  769.        atom == DEVICE_XATOM_SPACING (d) ||
  770.        atom == DEVICE_XATOM_CHARSET_REGISTRY (d) ||
  771.        atom == DEVICE_XATOM_CHARSET_ENCODING (d) ||
  772.        !strcmp (name_str, "CHARSET_COLLECTIONS") ||
  773.        !strcmp (name_str, "FONTNAME_REGISTRY") ||
  774.        !strcmp (name_str, "CLASSIFICATION") ||
  775.        !strcmp (name_str, "COPYRIGHT") ||
  776.        !strcmp (name_str, "DEVICE_FONT_NAME") ||
  777.        !strcmp (name_str, "FULL_NAME") ||
  778.        !strcmp (name_str, "MONOSPACED") ||
  779.        !strcmp (name_str, "QUALITY") ||
  780.        !strcmp (name_str, "RELATIVE_SET") ||
  781.        !strcmp (name_str, "RELATIVE_WEIGHT") ||
  782.        !strcmp (name_str, "STYLE")))
  783.     {
  784.       val_str = XGetAtomName (dpy, props [i].card32);
  785.       value = (val_str ? build_string (val_str) : Qnil);
  786.     }
  787.       else
  788.     value = make_number (props [i].card32);
  789.       if (name_str) XFree (name_str);
  790.       result = Fcons (Fcons (name, value), result);
  791.     }
  792.   return result;
  793. }
  794.  
  795. static Lisp_Object
  796. x_list_fonts (Lisp_Object pattern, Lisp_Object device)
  797. {
  798.   char **names;
  799.   int count = 0;
  800.   Lisp_Object result = Qnil;
  801.   
  802.   names = XListFonts (DEVICE_X_DISPLAY (XDEVICE (device)),
  803.               string_ext_data (XSTRING (pattern)),
  804.               MAX_FONT_COUNT, &count);
  805.   while (count--)
  806.     result = Fcons (build_string (names [count]), result);
  807.   if (names)
  808.     XFreeFontNames (names);
  809.   return result;
  810. }
  811.  
  812.  
  813. #ifdef EPOCH
  814.  
  815. /************************************************************************/
  816. /*                              X resources                             */
  817. /************************************************************************/
  818.  
  819. Lisp_Object Qx_resourcep;
  820. static Lisp_Object mark_x_resource (Lisp_Object, void (*) (Lisp_Object));
  821. static void print_x_resource (Lisp_Object, Lisp_Object, int);
  822. static void finalize_x_resource (void *, int);
  823. static int x_resource_equal (Lisp_Object o1, Lisp_Object o2, int depth);
  824. static unsigned long x_resource_hash (Lisp_Object obj, int depth);
  825. DEFINE_LRECORD_IMPLEMENTATION ("x-resource", x_resource,
  826.                    mark_x_resource, print_x_resource,
  827.                    finalize_x_resource, x_resource_equal,
  828.                    x_resource_hash, struct Lisp_X_Resource);
  829.  
  830. static Lisp_Object
  831. mark_x_resource (Lisp_Object obj, void (*markobj) (Lisp_Object))
  832. {
  833.   return Qnil;
  834. }
  835.  
  836. static void
  837. print_x_resource (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
  838. {
  839.   char buf[100];
  840.   char *default_string = "Resource";
  841.   Lisp_Object atom_symbol;
  842.   struct device *d = get_x_device (Qnil);
  843.  
  844.   if (print_readably)
  845.     error ("printing unreadable object #<x-resource 0x%x>",
  846.        XX_RESOURCE (obj)->xid);
  847.  
  848.   atom_symbol = x_atom_to_symbol (d, XX_RESOURCE (obj)->type);
  849.   sprintf (buf, "#<x-resource %s 0x%x>",
  850.        (NILP (atom_symbol)
  851.         ? default_string
  852.         : string_data (XSTRING (Fsymbol_name (atom_symbol)))),
  853.        XX_RESOURCE (obj)->xid);
  854.   write_c_string (buf, printcharfun);
  855. }
  856.  
  857. static void
  858. finalize_x_resource (void *header, int for_disksave)
  859. {
  860. }
  861.  
  862. static int
  863. x_resource_equal (Lisp_Object o1, Lisp_Object o2, int depth)
  864. {
  865.   return (XX_RESOURCE (o1)->xid == XX_RESOURCE (o2)->xid);
  866. }
  867.  
  868. static unsigned long
  869. x_resource_hash (Lisp_Object obj, int depth)
  870. {
  871.   return XX_RESOURCE (obj)->xid;
  872. }
  873.  
  874. /*
  875.  * Epoch equivalent:  epoch::resourcep
  876.  */
  877. DEFUN ("x-resource-p", Fx_resource_p, Sx_resource_p, 1, 1, 0,
  878.        "Return non-nil if OBJECT is an X resource object.")
  879.      (object)
  880.      Lisp_Object object;
  881. {
  882.   return (X_RESOURCEP (object) ? Qt : Qnil);
  883. }
  884.  
  885. /*
  886.  * Epoch equivalent:  epoch::set-resource-type
  887. */
  888. DEFUN ("x-set-x-resource-type", Fx_set_x_resource_type, Sx_set_x_resource_type,
  889.        2, 2, 0,
  890.   "Set the type of RESOURE to TYPE.  The new type must be an atom.")
  891.      (resource, type)
  892.      Lisp_Object resource, type;
  893. {
  894.   CHECK_X_RESOURCE (resource, 0);
  895.   CHECK_X_RESOURCE (type, 0);
  896.  
  897.   if (XX_RESOURCE (type)->type != XA_ATOM)
  898.     error ("New type must be an atom");
  899.  
  900.   XX_RESOURCE (resource)->type = XX_RESOURCE (type)->xid;
  901.   return resource;
  902. }
  903.  
  904. #endif /* EPOCH */
  905.  
  906.  
  907. /************************************************************************/
  908. /*                            initialization                            */
  909. /************************************************************************/
  910.  
  911. void
  912. syms_of_objects_x (void)
  913. {
  914. #ifdef EPOCH
  915.   defsymbol (&Qx_resourcep, "x-resource-p");
  916.   defsubr (&Sx_resource_p);
  917.   defsubr (&Sx_set_x_resource_type);
  918. #endif /* EPOCH */
  919. }
  920.  
  921. void
  922. device_type_create_objects_x (void)
  923. {
  924.   /* object methods */
  925.  
  926.   DEVICE_HAS_METHOD (x, initialize_color_instance);
  927.   DEVICE_HAS_METHOD (x, print_color_instance);
  928.   DEVICE_HAS_METHOD (x, finalize_color_instance);
  929.   DEVICE_HAS_METHOD (x, color_instance_equal);
  930.   DEVICE_HAS_METHOD (x, color_instance_hash);
  931.   DEVICE_HAS_METHOD (x, color_instance_rgb_components);
  932.   DEVICE_HAS_METHOD (x, valid_color_name_p);
  933.  
  934.   DEVICE_HAS_METHOD (x, initialize_font_instance);
  935.   DEVICE_HAS_METHOD (x, mark_font_instance);
  936.   DEVICE_HAS_METHOD (x, print_font_instance);
  937.   DEVICE_HAS_METHOD (x, finalize_font_instance);
  938.   DEVICE_HAS_METHOD (x, font_instance_truename);
  939.   DEVICE_HAS_METHOD (x, font_instance_properties);
  940.   DEVICE_HAS_METHOD (x, list_fonts);
  941. }
  942.  
  943. void
  944. vars_of_objects_x (void)
  945. {
  946.   DEFVAR_BOOL ("x-handle-non-fully-specified-fonts",&handle_nonfull_spec_fonts,
  947.     "If this is true then fonts which do not have all characters specified\n\
  948. will be considered to be proportional width even if they are actually\n\
  949. fixed-width.  If this is not done then characters which are supposed to\n\
  950. have 0 width may appear to actually have some width.\n\
  951. \n\
  952. Note:  While setting this to t guarantees correct output in all\n\
  953. circumstances, it also causes a noticeable performance hit when using\n\
  954. fixed-width fonts.  Since most people don't use characters which could\n\
  955. cause problems this is set to nil by default.");
  956.   handle_nonfull_spec_fonts = 0;
  957. }
  958.  
  959.  
  960. void
  961. Xatoms_of_objects_x (struct device *d)
  962. {
  963. #define ATOM(x) XInternAtom (DEVICE_X_DISPLAY (d), (x), False)
  964.  
  965.   DEVICE_XATOM_FOUNDRY (d) = ATOM ("FOUNDRY");
  966.   DEVICE_XATOM_FAMILY_NAME (d) = ATOM ("FAMILY_NAME");
  967.   DEVICE_XATOM_WEIGHT_NAME (d) = ATOM ("WEIGHT_NAME");
  968.   DEVICE_XATOM_SLANT (d) = ATOM ("SLANT");
  969.   DEVICE_XATOM_SETWIDTH_NAME (d) = ATOM ("SETWIDTH_NAME");
  970.   DEVICE_XATOM_ADD_STYLE_NAME (d) = ATOM ("ADD_STYLE_NAME");
  971.   DEVICE_XATOM_PIXEL_SIZE (d) = ATOM ("PIXEL_SIZE");
  972.   DEVICE_XATOM_POINT_SIZE (d) = ATOM ("POINT_SIZE");
  973.   DEVICE_XATOM_RESOLUTION_X (d) = ATOM ("RESOLUTION_X");
  974.   DEVICE_XATOM_RESOLUTION_Y (d) = ATOM ("RESOLUTION_Y");
  975.   DEVICE_XATOM_SPACING (d) = ATOM ("SPACING");
  976.   DEVICE_XATOM_AVERAGE_WIDTH (d) = ATOM ("AVERAGE_WIDTH");
  977.   DEVICE_XATOM_CHARSET_REGISTRY (d) = ATOM ("CHARSET_REGISTRY");
  978.   DEVICE_XATOM_CHARSET_ENCODING (d) = ATOM ("CHARSET_ENCODING");
  979. }
  980.